#import "DeleteAisleDialogController.h"
#import "RNAisle.h"
#import "DetailViewController.h"

NSInteger const kDeleteAisle = 1;
NSInteger const kCancelDeleteAisle = 2;

@implementation DeleteAisleDialogController

@synthesize aisle = aisle_;
@synthesize moveItemsButton = moveItemsButton_;
@synthesize aisleController = aisleController_;
@synthesize detailViewController = detailViewController_;

-(void)confirmDeleteAisle:(RNAisle *)aisleToDelete {
    [self setAisle:aisleToDelete];
    [NSApp beginSheet:[self window] modalForWindow:[NSApp mainWindow] modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:nil];
}

- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
    [[self window] orderOut:self];
    if (returnCode == kCancelDeleteAisle)
        return;
    
    if ([[self moveItemsButton] state] == NSOnState) {
        RNAisle *destinationAisle = [[[self aisleController] selectedObjects] objectAtIndex:0];
        [destinationAisle addItems:[[self aisle] items]];
    }
    [[self managedObjectContext] deleteObject:[self aisle]];
    [[self managedObjectContext] processPendingChanges];
    [[self detailViewController] reloadDetailView];
}

-(IBAction)deleteAisle:(id)sender {
    [NSApp endSheet:[self window] returnCode:kDeleteAisle];
}

-(IBAction)cancelDeleteAisle:(id)sender {
    [NSApp endSheet:[self window] returnCode:kCancelDeleteAisle];
}

-(NSPredicate *)arrayControllerFilterPredicate {
    return [NSPredicate predicateWithFormat:@"store == %@", [[self aisle] store]];
}

@end
